<?php

namespace app\index\controller;

class Shop extends Base
{
    // 返回商城头图
    public function getMallpic()
    {
        $list = db('mallpic')->where(['status' => 1,'on' => 1])->field('id,pic_name')->select();
        $this->result($list);
    }

    // 返回商品分类
    public function getCate()
    {
        $list = db('goods_cate')->field('id,cate_pic,cate_name')->select();
        $this->result($list);
    }

    // 商品列表
    public function getGoods()
    {
        $map = [];
        $map['status'] = 1;
        if (input('param.shopuid')) {
            $map['shop_id'] = input('param.shopuid');
        }
        if (input('param.cate_id')) {
            $map['cate_id'] = input('param.cate_id');
        }
        if (input('param.keywords')) {
            $map['goods_name'] = ['like', '%'.input('param.keywords').'%'];
        }

        $list = model('Goods')->getList($map, input('param.size'));
        $this->result($list);
    }

    // 返回商品详情
    public function goodsInfo()
    {
        $goods_id = input('param.goods_id');
        if ($goods_id) {
            $info = model('Goods')->getInfo($goods_id);
            $this->success($info);
        }
        $this->error('无商品id');
    }

    // 添加商品
    public function addGoods()
    {
        $post = input('post.');
        $img = [];
        if ($post['goodsimg']) {
            foreach ($post['goodsimg'] as $k => $v) {
               $img[$k] = base64_imgup($v['url'],'goods');
            }
        }

        $data = [
            'cate_id' => $post['goodsclass'],
            'shop_id' => $post['shopid'] ,
            'goods_pic'  => $img[0],
            'goods_pics' => implode('#', $img),
            'goods_name' => $post['goodsname'],
            'goods_note' => $post['goodsde'],
            'sku_list' =>  [
                [
                    'sku_name' => $post['goodsguige'],
                    'realprice'  => $post['goodsnewPrice'],
                    'oldprice'   => $post['goodsoldPrice'],
                    'sales' => 0,
                ],
            ],
        ];

        $rst =  model('Goods')->addInfo($data);

        $this->success($rst);
    }

    // 编辑商品
    public function eidtGoods()
    {

    }

    // 商品下架
    public function deleteGoods()
    {

    }

}
